﻿using HML.Design;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;

namespace HML
{
    /// <summary>
    ///ImageSlide选项集合
    /// </summary>
    [Editor(typeof(CollectionTextHelpEditor), typeof(UITypeEditor))]
    public class ImageSlideImageItemCollection : IList, ICollection, IEnumerable
    {
        #region

        private ArrayList imageSliderImageItemList = new ArrayList();

        private ImageSlide owner;

        #endregion

        internal ImageSlideImageItemCollection(ImageSlide owner)
        {
            this.owner = owner;
        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            ImageSlideItem[] listArray = new ImageSlideItem[this.imageSliderImageItemList.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (ImageSlideItem)this.imageSliderImageItemList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.imageSliderImageItemList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.imageSliderImageItemList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is ImageSlideItem))
            {
                throw new ArgumentException("ImageSlideItem");
            }
            return this.Add((ImageSlideItem)value);
        }

        public int Add(ImageSlideItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (item.Owner != null)
            {
                throw new ArgumentException("ImageSlideItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            item.Owner = this.owner;
            this.imageSliderImageItemList.Add(item);
            if (this.owner != null)
            {
                if ((this.Count <= this.owner.FrameAmount + 2) || this.owner.IsInImageFrame(item))
                {
                    this.owner.SetImageDefaultIndex();
                    this.owner.UpdateImageFrameInfo();
                    this.owner.Invalidate();
                }
            }
            return this.Count - 1;
        }

        public void Clear()
        {
            for (int i = 0; i < this.imageSliderImageItemList.Count; i++)
            {
                ((ImageSlideItem)this.imageSliderImageItemList[i]).Owner=null;
            }
            this.imageSliderImageItemList.Clear();

            if (this.owner != null)
            {
                this.owner.SetImageDefaultIndex();
                this.owner.UpdateImageFrameInfo();
                this.owner.Invalidate();
            }
        }

        public bool Contains(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object item)
        {
            if (item is ImageSlideItem)
            {
                return this.Contains((ImageSlideItem)item);
            }
            return false;
        }

        public int IndexOf(object item)
        {
            if (item is ImageSlideItem)
            {
                return this.imageSliderImageItemList.IndexOf(item);
            }
            return -1;
        }

        public void Insert(int index, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is ImageSlideItem))
            {
                throw new ArgumentException("类型应该为 ImageSlideItem");
            }
            ImageSlideItem item = (ImageSlideItem)value;
            if (item.Owner != null)
            {
                throw new ArgumentException("ImageSlideItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            if (index > this.Count)
            {
                index = this.Count;
            }
            ArrayList tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                tmp.Add(this.imageSliderImageItemList[i]);
            }
            item.Owner = this.owner;
            tmp.Add(value);
            for (int i = index; i < this.Count; i++)
            {
                tmp.Add(this.imageSliderImageItemList[i]);
            }
            this.imageSliderImageItemList = tmp;

            if (this.owner != null)
            {
                this.owner.SetImageDefaultIndex();
                this.owner.UpdateImageFrameInfo();
                this.owner.Invalidate();
            }
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is ImageSlideItem))
            {
                throw new ArgumentException("ImageSlideItem");
            }
            this.Remove((ImageSlideItem)value);
        }

        public void Remove(ImageSlideItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            item.Owner = null;
            this.imageSliderImageItemList.Remove(item);

            if (this.owner != null)
            {
                this.owner.SetImageDefaultIndex();
                this.owner.UpdateImageFrameInfo();
                this.owner.Invalidate();
            }
        }

        public void RemoveAt(int index)
        {
            ((ImageSlideItem)this.imageSliderImageItemList[index]).Owner = null;
            this.imageSliderImageItemList.RemoveAt(index);

            if (this.owner != null)
            {
                this.owner.SetImageDefaultIndex();
                this.owner.UpdateImageFrameInfo();
                this.owner.Invalidate();
            }
        }

        public ImageSlideItem this[int index]
        {
            get { return (ImageSlideItem)this.imageSliderImageItemList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (value.Owner != null)
                {
                    throw new ArgumentException("ImageSlideItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
                }

                ((ImageSlideItem)this.imageSliderImageItemList[index]).Owner=null;
                value.Owner = this.owner;
                this.imageSliderImageItemList[index] = value;

                if (this.owner != null)
                {
                    if (this.owner.IsNegative(this.owner.Orientation))
                    {
                        if (this.owner.Index - 1 <= index && index <= this.owner.Index + this.owner.FrameAmount - 1)
                        {
                            this.owner.SetImageDefaultIndex();
                            this.owner.UpdateImageFrameInfo();
                            this.owner.Invalidate();
                        }
                    }
                    else
                    {
                        if (this.owner.Index - this.owner.FrameAmount + 1 <= index && index <= this.owner.Index + 1)
                        {
                            this.owner.SetImageDefaultIndex();
                            this.owner.UpdateImageFrameInfo();
                            this.owner.Invalidate();
                        }
                    }
                }
            }
        }

        object IList.this[int index]
        {
            get { return (object)this.imageSliderImageItemList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (!(value is ImageSlideItem))
                {
                    throw new ArgumentException("类型应该为 ImageSlideItem");
                }
                if (((ImageSlideItem)value).Owner != null)
                {
                    throw new ArgumentException("ImageSlideItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
                }

                ((ImageSlideItem)this.imageSliderImageItemList[index]).Owner=null;
                ((ImageSlideItem)value).Owner = this.owner;
                this.imageSliderImageItemList[index] = value;

                if (this.owner != null)
                {
                    if (this.owner.IsNegative(this.owner.Orientation))
                    {
                        if (this.owner.Index - 1 <= index && index <= this.owner.Index + this.owner.FrameAmount - 1)
                        {
                            this.owner.SetImageDefaultIndex();
                            this.owner.UpdateImageFrameInfo();
                            this.owner.Invalidate();
                        }
                    }
                    else
                    {
                        if (this.owner.Index - this.owner.FrameAmount + 1 <= index && index <= this.owner.Index + 1)
                        {
                            this.owner.SetImageDefaultIndex();
                            this.owner.UpdateImageFrameInfo();
                            this.owner.Invalidate();
                        }
                    }
                }
            }
        }

        #endregion
    }

}
